﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using Data.Enum;
using DataAccess.AccountManage;
using Ninject;
using WinForm.Helper;

namespace WinForm.Analysis
{
    public partial class AnalysisData : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly IBusinesses _business = Kernel.Get<IBusinesses>();

        public AnalysisData()
        {
            InitializeComponent();

            var title1 = new Title { Text = "业务笔数" };
            chtBiz.Titles.Add(title1);

            chtBiz.ChartAreas.Add("chtArea1");
            chtBiz.Series.Add("series1");

            //chtBiz chartarea

            chtBiz.ChartAreas["chtArea1"].Area3DStyle.Enable3D = true;
            chtBiz.ChartAreas["chtArea1"].Area3DStyle.Rotation = 40;
            chtBiz.ChartAreas["chtArea1"].Area3DStyle.Inclination = 50;
            chtBiz.ChartAreas["chtArea1"].Area3DStyle.PointDepth = 30;
            chtBiz.ChartAreas["chtArea1"].Area3DStyle.WallWidth = 0;
            chtBiz.ChartAreas["chtArea1"].Area3DStyle.LightStyle = LightStyle.Realistic;
            chtBiz.ChartAreas["chtArea1"].BackColor = Color.FromArgb(240, 240, 240);

            chtBiz.Series["series1"].ChartType = SeriesChartType.Pie;
            chtBiz.Series["series1"].Label = "#VALX: #VAL";
            chtBiz.Series["series1"]["PieLabelStyle"] = "Outside";//数值显示在圆饼外
            chtBiz.Series["series1"]["PieDrawingStyle"] = "Default";
            chtBiz.Series["series1"].IsValueShownAsLabel = true;

        }

        private void AnalysisData_Load(object sender, EventArgs e)
        {
            var xyValues = GetXYValues(DateTime.Now);
            SetChart(xyValues.Keys.First(), xyValues.Values.First());
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            var xyValues = GetXYValues(dtStartTimer.Value, dtEndTimer.Value);
            SetChart(xyValues.Keys.First(), xyValues.Values.First());
        }

        private void SetChart(IEnumerable<string> xValues, IEnumerable<int> yValues)
        {
            chtBiz.Series["series1"].Points.DataBindXY(xValues, yValues);
        }

        /// <summary>
        /// 根据时间获取数据
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        private Dictionary<List<string>, List<int>> GetXYValues(DateTime time)
        {
            var data = _business.GetBusinessCount(time);
            var xValues = new List<string>();
            var yValues = new List<int>();

            if (data.ResultNumber == ResultEnum.Success)
            {
                if (data.Obj.Count == 0)
                {
                    xValues.Add("暂无成交数据");
                    yValues.Add(0);
                }
                else
                {
                    foreach (var d in data.Obj)
                    {
                        xValues.Add(d.businessname);
                        yValues.Add(d.count);
                    }
                }
            }

            var result = new Dictionary<List<string>, List<int>> { { xValues, yValues } };
            return result;
        }

        private Dictionary<List<string>, List<int>> GetXYValues(DateTime startTime, DateTime endTime)
        {
            var data = _business.GetBusinessCount(startTime, endTime);
            var xValues = new List<string>();
            var yValues = new List<int>();

            if (data.ResultNumber == ResultEnum.Success)
            {
                if (data.Obj.Count == 0)
                {
                    xValues.Add("暂无成交数据");
                    yValues.Add(0);
                }
                else
                {
                    foreach (var d in data.Obj)
                    {
                        xValues.Add(d.businessname);
                        yValues.Add(d.count);
                    }
                }
            }

            var result = new Dictionary<List<string>, List<int>> { { xValues, yValues } };
            return result;
        }
    }
}
